'''
@Project ：pythonApiTest 
@File    ：ExcelManager.py
@IDE     ：PyCharm 
@Author  ：胡文
@Date    ：2025/2/28 上午9:34 
'''
import json

from openpyxl import load_workbook


class ExcelManager:
    def __init__(self, file_path):
        self.wb = load_workbook(file_path)

    def read_cases(self):
        cases = []
        for sheet in self.wb.worksheets:
            for row in sheet.iter_rows(min_row=2, values_only=True):
                case = {
                    "case_id": row[0],
                    "path": row[1],
                    "method": row[2],
                    "params": self._parse_params(row[3]),
                    "expected_status": row[4],
                    "expected_response": row[5],
                    "precondition": row[6]
                }
                cases.append(case)
        return cases

    def _parse_params(self, param_str):
        """将JSON字符串转换为Python字典"""
        return json.loads(param_str)